<?php
declare (strict_types=1);

namespace app\index\controller;

use addons\V2\admin\model\OrderTemporary;
use addons\V2\admin\model\Product as ProductModel;
use addons\V2\admin\model\ProductCate;
use addons\V2\admin\model\ProductSpecData;
use app\common\model\Illustrate;
use \addons\V2\admin\model\ProductSpecValue;
use app\common\model\Spec;
use app\common\model\SpecItem;
use app\common\model\User;
use app\common\tool\Result;
use think\facade\Cache;
use think\facade\View;
use think\Response;

class Product extends Base
{

    public function search()
    {
        $history = Cache::get('searchHistory_' . $this->user->id);
        if ($history) {
            $history = json_decode($history, true);
            $news = [];
            for ($i = count($history); $i > 0; $i--) {
                $news[] = $history[$i - 1];
            }
            $history = $news;
        }
        View::assign('history', $history ?: []);
        return View::fetch();
    }

    public function emptySearch()
    {
        Cache::delete('searchHistory_' . $this->user->id);
        return Result::success('已清空');
    }


    public function index()
    {
        $cate_1 = $this->request->get('cate_1');
        $keyword = $this->request->get('keyword');
        $cate = ProductCate::where('id', $cate_1)->value('title');
        View::assign([
            'title'   => $keyword ?: $cate,
            'cate_1'  => $cate_1,
            'keyword' => $keyword
        ]);
        return View::fetch();
    }

    public function details($id)
    {
        $info = ProductModel::where(['status' => 1, 'id' => $id])->find();
        if($info->cate_1==2)
        {
            if($this->user->level_id)
            {
                $price = 'price_'.$this->user->level_id;
                $info->vip_price = $info->$price;
            }else{
                $info->vip_price = $info->price;
            }
        }
        $info->SpecData = ProductSpecData::getSpecData($info->id);
//        dump($info->SpecData);die;
        if (empty($info)) return redirect('/');
        $info->images = explode(',', $info->images);
        View::assign([
            'info'  => $info,
            'count' => \app\common\model\Cart::where('uid', $this->user->id)->sum('num')
        ]);
        return View::fetch();
    }


    public function getList(): Response
    {
        $param = $this->request->get();
        $map = [];
        foreach ($param as $key => $value) {
            if (!in_array($key, ['page', 'type', 'keyword']) && $value) {
                $map[$key] = $value;
            }
        }
        $like = [];
        if (isset($param['keyword']) && $param['keyword']) {
            $like['field'] = 'title';
            $like['keyword'] = $param['keyword'];
            $history = Cache::get('searchHistory_' . $this->user->id);
            if ($history) {
                $history = json_decode($history, true);
                if (!in_array($param['keyword'], $history)) {
                    $history[] = $param['keyword'];
                    Cache::set('searchHistory_' . $this->user->id, json_encode($history));
                }
            } else {
                $history[] = $param['keyword'];
                Cache::set('searchHistory_' . $this->user->id, json_encode($history));
            }
        }
        $product = ProductModel::getPageList($map, 'id,title,cate_1,price,vip_price,price_1,price_2,price_3,price_4,price_5,price_6,icon', $param['page'], 20, true, '', '', ['key' => 'sort', 'value' => 'asc'], $like);
        foreach ($product['data'] as $k=>$v)
        {
           if($v->cate_1==2)
           {
               if($this->user->level_id)
               {
                   $price = 'price_'.$this->user->level_id;
                   $v->vip_price = $v->$price;
               }else{
                   $v->vip_price = $v->price;
               }

           }
        }
        return Result::fastReturn($product);
    }

    public function getProductSpec()
    {
        $param = $this->request->param();

        $data= ProductSpecValue::where(['pid'=>$param['pid'],'sku_id'=>$param['sku_id'],'delete_time'=>0])->field('id,sku_id,pid,price,icon,vip_price,price_1,price_2,price_3,price_4,price_5,price_6')->find();
        $product = \addons\V2\admin\model\Product::find($param['pid']);
        if($product->cate_1==2)
        {
            if($this->user->level_id)
            {
                $price = 'price_'.$this->user->level_id;
                $data->vip_price = $data->$price;
            }else{
                $data->vip_price = $data->price;
            }
        }

        if($data)
        {
            return Result::fastReturn(['code' => 1, 'data'=>$data]);
        }else{
            return Result::fastReturn(['code' => -1, 'data'=>0]);
        }

    }

    public function submit(): Response
    {
        return Result::fastReturn(OrderTemporary::setTemporary($this->request->post(), $this->user));
    }
}

